package cn.zhuama.system.service.impl;

import cn.zhuama.system.domain.Role;
import cn.zhuama.system.mapper.EmployeeRoleMapper;
import cn.zhuama.system.mapper.RoleMapper;
import cn.zhuama.system.mapper.RoleMenuMapper;
import cn.zhuama.system.mapper.RolePermissionMapper;
import cn.zhuama.system.service.IRoleService;
import cn.zhuama.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务实现类：
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private EmployeeRoleMapper employeeRoleMapper;
    @Override
    public void save(Role role) {
        roleMapper.insert(role);
        roleMenuMapper.insertRoleAndMenu(role.getId(),role.getMenus());
        rolePermissionMapper.insertRoleAndPermission(role.getId(),role.getPermissions());
    }
    @Override
    @Transactional
    public void removeById(Long id) {
        roleMenuMapper.deleteByRoleId(id);
        rolePermissionMapper.deleteByRoleId(id);
        employeeRoleMapper.deleteByRoleId(id);
        roleMapper.deleteById(id);
    }
    @Override
    @Transactional
    public void editById(Role role) {
       super.editById(role);
       roleMenuMapper.deleteByRoleId(role.getId());
       rolePermissionMapper.deleteByRoleId(role.getId());
       roleMenuMapper.insertRoleAndMenu(role.getId(),role.getMenus());
       rolePermissionMapper.insertRoleAndPermission(role.getId(),role.getPermissions());
    }
}
